import { createRouter, createWebHistory, RouteRecordRaw, createWebHashHistory } from 'vue-router';

import { indexRouter } from './index.router';
import { userRouter } from './user.router';
import { demoRouter } from './demo.router'; // 示例
import { baseDataRouter } from './baseData.router'; // 基础数据
import { fileManageRouter } from './fileManage.router'; // 文件管理
import { archivesManageRouter } from './archivesManage.router'; // 档案管理
import { permissionManageRouter } from './permissionManage.router'; // 权限管理
import { logManageRouter } from './logManage.router'; // 日志管理
import { searchManageRouter } from './searchManage.router'; // 高级查询
import { statementRouter } from './statement.router'; // 报表

import { RouteLocationNormalized, NavigationGuardNext } from 'vue-router';

import { qiankunWindow } from 'vite-plugin-qiankun/dist/helper';
import { hooks } from './hook';

const routes: any = [
  ...userRouter,
  ...indexRouter,
  ...baseDataRouter,
  ...fileManageRouter,
  ...archivesManageRouter,
  ...permissionManageRouter,
  ...logManageRouter,
  ...searchManageRouter,
  ...statementRouter
];
const dynamicMenu: any = [...baseDataRouter, ...fileManageRouter, ...archivesManageRouter, ...permissionManageRouter, ...logManageRouter, ...searchManageRouter, ...statementRouter]; //有权限的路由

const router: any = createRouter({
  history: createWebHistory(qiankunWindow.__POWERED_BY_QIANKUN__ ? import.meta.env.VITE_QIANKUN_PATH + import.meta.env.VITE_PUBLIC_PATH : import.meta.env.VITE_PUBLIC_PATH),
  routes: routes
});

router.beforeEach((to, from, next) => {
  hooks.loginPermission(to, from, next);
});

// 过滤路由
function filterRouter(data) {
  const list = [];
  for (let i = 0; i < data.length; i++) {
    if (data[i].meta && !data[i].meta.hidden) {
      if (data[i].children && data[i].children.length > 0) {
        data[i].children = filterRouter(data[i].children);
      }
      list.push(data[i]);
    }
  }
  return list;
}
const routesMenu: any = filterRouter(routes);
export { routesMenu, dynamicMenu };
export default router;
